#include "Extractor.h"

void Extractor::extract_extra(const vector<string> &words, int cur, vector<string> &features)
{
	string x_3 = (cur>=3) ? words[cur-3] : "<s>";
	string x_2 = (cur>=2) ? words[cur-2] : "<s>";
	string x_1 = (cur>=1) ? words[cur-1] : "<s>";
	string x0  = words[cur];
	string x1  = (cur<(int)words.size()-1) ? words[cur+1] : "<t>";
	string x2  = (cur<(int)words.size()-2) ? words[cur+2] : "<t>";
	string x3  = (cur<(int)words.size()-3) ? words[cur+3] : "<t>";

	// cardinal window

	string Cardi_1 = is_Cardinal(x_1) ? "1" : "0";
	features.push_back("Cardi_1="+Cardi_1);

	string Cardi_2 = is_Cardinal(x_2) ? "1" : "0";
	features.push_back("Cardi_2="+Cardi_2);

	string Cardi_3 = is_Cardinal(x_3) ? "1" : "0";
	features.push_back("Cardi_3="+Cardi_3);

	string Cardi0 = is_Cardinal(x0) ? "1" : "0";
	features.push_back("Cardi0="+Cardi0);

	string Cardi1 = is_Cardinal(x1) ? "1" : "0";
	features.push_back("Cardi1="+Cardi1);

	string Cardi2 = is_Cardinal(x2) ? "1" : "0";
	features.push_back("Cardi2="+Cardi2);

	string Cardi3 = is_Cardinal(x3) ? "1" : "0";
	features.push_back("Cardi3="+Cardi3);


	if(is_ORG_end(words,cur)) features.push_back("ORG-end");
	if(is_LOC_end(words,cur)) features.push_back("LOC-end");
}

///////////////////////////////////////////////////////////////

void Extractor::extract_Tag(const vector<string> words, int cur, 
							const string& t_2, const string& t_1, vector<string>& features)
{
	string x_2 = (cur>=2) ? words[cur-2] : "<s>";
	string x_1 = (cur>=1) ? words[cur-1] : "<s>";
	string x0  = words[cur];
	string x1  = (cur<(int)words.size()-1) ? words[cur+1] : "<t>";
	string x2  = (cur<(int)words.size()-2) ? words[cur+2] : "<t>";

	//(1) previous two predictions t-1 , t-2

	features.push_back("t_1="+t_1);
	features.push_back("t_2="+t_2);

	//(7) conjunction of window C and t-1

	string x2__t_1 = x2+"|"+t_1;
	features.push_back("x2|t_1="+x2__t_1);

	string x1__t_1 = x1+"|"+t_1;
	features.push_back("x1|t_1="+x1__t_1);

	string x0__t_1 = x0+"|"+t_1;
	features.push_back("x0|t_1="+x0__t_1);

	string x_1__t_1 = x_1+"|"+t_2;
	features.push_back("x_1|t_1="+x_1__t_1);

	string x_2__t_1 = x_2+"|"+t_2;
	features.push_back("x_2|t_1="+x_2__t_1);
}

void Extractor::extract(const vector<string>& words, int i, vector<string>& features)
{
	features.clear();

	string x_3 = (i >= 3) ? words[i-3] : "<s>";
	string x_2 = (i >= 2) ? words[i-2] : "<s>";
	string x_1 = (i >= 1) ? words[i-1] : "<s>";
	string x0 = words[i];
	string x1 = (i < (int)words.size()-1) ? words[i+1] : "<t>";
	string x2 = (i < (int)words.size()-2) ? words[i+2] : "<t>";
	string x3 = (i < (int)words.size()-3) ? words[i+3] : "<t>";

	//(2) current word x0

	features.push_back("cur="+x0);

	//(5) tokens in the window c

	features.push_back("x_3="+x_3);
	features.push_back("x_2="+x_2);
	features.push_back("x_1="+x_1);
	features.push_back("x0="+x0);
	features.push_back("x1="+x1);
	features.push_back("x2="+x2);
	features.push_back("x3="+x3);

	//(F-1) bi-gram tokens in window c

	features.push_back("x_3|x_2="+x_3+"|"+x_2);
	features.push_back("x_2|x_1="+x_2+"|"+x_1);
	features.push_back("x_1|x0="+x_1+"|"+x0);
	features.push_back("x0|x1="+x0+"|"+x1);
	features.push_back("x1|x2="+x1+"|"+x2);
	features.push_back("x2|x3="+x2+"|"+x3);
	features.push_back("x_1|x1="+x_1+"|"+x1);


	/*
	//(6) capitalization pattern in the window c

	string InitCap_1 = is_InitCap(x_1) ? "1" : "0";
	features.push_back("InitCap_1="+InitCap_1);

	string InitCap_2 = is_InitCap(x_2) ? "1" : "0";
	features.push_back("InitCap_2="+InitCap_2);

	string InitCap_3 = is_InitCap(x_3) ? "1" : "0";
	features.push_back("InitCap_3="+InitCap_3);

	string InitCap0 = is_InitCap(x0) ? "1" : "0";
	features.push_back("InitCap0="+InitCap0);

	string InitCap1 = is_InitCap(x1) ? "1" : "0";
	features.push_back("InitCap1="+InitCap1);

	string InitCap2 = is_InitCap(x2) ? "1" : "0";
	features.push_back("InitCap2="+InitCap2);

	string InitCap3 = is_InitCap(x3) ? "1" : "0";
	features.push_back("InitCap3="+InitCap3);


	//(3) word type information

	// x-1
	string AllCap_1 = is_AllCap(x_1) ? "1" : "0";
	features.push_back("AllCap_1="+AllCap_1);

	string AllNoneLetter_1 = is_AllNoneLetter(x_1) ? "1" : "0";
	features.push_back("AllNoneLetter_1="+AllNoneLetter_1);

	string AllDigit_1 = is_AllDigit(x_1) ? "1" : "0";
	features.push_back("AllDigit_1="+AllDigit_1);

	// x-2
	string AllCap_2 = is_AllCap(x_2) ? "1" : "0";
	features.push_back("AllCap_2="+AllCap_2);

	string AllNoneLetter_2 = is_AllNoneLetter(x_2) ? "1" : "0";
	features.push_back("AllNoneLetter_2="+AllNoneLetter_2);

	string AllDigit_2 = is_AllDigit(x_2) ? "1" : "0";
	features.push_back("AllDigit_2="+AllDigit_2);

	// x-3
	string AllCap_3 = is_AllCap(x_3) ? "1" : "0";
	features.push_back("AllCap_3="+AllCap_3);

	string AllNoneLetter_3 = is_AllNoneLetter(x_3) ? "1" : "0";
	features.push_back("AllNoneLetter_3="+AllNoneLetter_3);

	string AllDigit_3 = is_AllDigit(x_3) ? "1" : "0";
	features.push_back("AllDigit_3="+AllDigit_3);

	// x0
	string AllCap0 = is_AllCap(x0) ? "1" : "0";
	features.push_back("AllCap0="+AllCap0);

	string AllNoneLetter0 = is_AllNoneLetter(x0) ? "1" : "0";
	features.push_back("AllNoneLetter0="+AllNoneLetter0);

	string AllDigit0 = is_AllDigit(x0) ? "1" : "0";
	features.push_back("AllDigit0="+AllDigit0);

	// x1
	string AllCap1 = is_AllCap(x1) ? "1" : "0";
	features.push_back("AllCap1="+AllCap1);

	string AllNoneLetter1 = is_AllNoneLetter(x1) ? "1" : "0";
	features.push_back("AllNoneLetter1="+AllNoneLetter1);

	string AllDigit1 = is_AllDigit(x1) ? "1" : "0";
	features.push_back("AllDigit1="+AllDigit1);

	// x2
	string AllCap2 = is_AllCap(x2) ? "1" : "0";
	features.push_back("AllCap2="+AllCap2);

	string AllNoneLetter2 = is_AllNoneLetter(x2) ? "1" : "0";
	features.push_back("AllNoneLetter2="+AllNoneLetter2);

	string AllDigit2 = is_AllDigit(x2) ? "1" : "0";
	features.push_back("AllDigit2="+AllDigit2);

	// x3
	string AllCap3 = is_AllCap(x3) ? "1" : "0";
	features.push_back("AllCap3="+AllCap3);

	string AllNoneLetter3 = is_AllNoneLetter(x3) ? "1" : "0";
	features.push_back("AllNoneLetter3="+AllNoneLetter3);

	string AllDigit3 = is_AllDigit(x3) ? "1" : "0";
	features.push_back("AllDigit3="+AllDigit3);

	//(4) prefixes and suffixes of x0

	for(int i=3; i<=4; i++)
		if((int)x0.length() > i)
		{
			string val = fs_algo::prefix(x0,i);
			features.push_back("Prefix"+fs_algo::i2s(i)+"="+val);
		}

	for(int i=1; i<=4; i++)
		if((int)x0.length() > i)
		{
			string val = fs_algo::suffix(x0,i);
			features.push_back("Suffix"+fs_algo::i2s(i)+"="+val);
		}
		*/
}

